/**
 * @param {character[][]} grid
 * @return {number}
 */
var numIslands = function(grid) {
  let count = 0;
  function deepSearch(i, j) {
    if(grid[i][j] == 1) {
      grid[i][j] = 0;
    } else {
      return;
    }
    if(i + 1 < grid.length) {
      deepSearch(i + 1, j);
    }
    if(i - 1 >= 0) {
      deepSearch(i - 1, j);
    }
    if(j + 1 < grid[i].length) {
      deepSearch(i, j + 1);
    }
    if(j - 1 >= 0) {
      deepSearch(i, j - 1);
    }
  }
  for(let i = 0; i < grid.length; i++) {
    for(let j = 0; j < grid[i].length; j++) {
      if(grid[i][j] == 0) {
        continue;
      } else {
        deepSearch(i, j);
        count++;
      }
    }
  }
  return count;
};
let grid = [
  ["1","1","1","1","0"],
  ["1","1","0","1","0"],
  ["1","1","0","0","0"],
  ["0","0","0","0","0"]
];
console.log(numIslands(grid));
grid = [
  ["1","1","0","0","0"],
  ["1","1","0","0","0"],
  ["0","0","1","0","0"],
  ["0","0","0","1","1"]
];
console.log(numIslands(grid));
